﻿using System;
using System.Collections.Generic;
using System.Text;
using LIU.Framework.Common.Extend;
using LIU.Framework.Core.Inject;
using NLog;

namespace LIU.Framework.Core.Log
{
    public class LogBase : ILogger, IDefaultImplementation
    {
        /// <summary>
        /// 场景
        /// </summary>
        private readonly string Scene;

        /// <summary>
        /// 每个文件最大大小 （默认5MB）
        /// </summary>
        private const long MaxSize = 1024 * 1024 * 5;
        public LogBase()
        {
            Scene = "default";
            CinfigLog();
        }
        public LogBase(string _scene)
        {
            Scene = _scene;
            CinfigLog();
        }

        private NLog.Logger Logger = null;

        private string LogDate;

        /// <summary>
        /// 配置日志
        /// </summary>
        private void CinfigLog()
        {
            var config = new NLog.Config.LoggingConfiguration();
            LogDate = DateString();
            string path = $@"{AppContext.BaseDirectory}\logs\{Scene}\";
            var logfile = new NLog.Targets.FileTarget()
            {
                FileName = path + $"{LogDate}.log",
                ArchiveFileName = path + $"{LogDate}.{{#}}.log",//分割文件名称
                ArchiveAboveSize = MaxSize,//分割文件大小 字节为单位
                Name = Scene,
                Encoding = Encoding.UTF8
            };
            var logconsole = new NLog.Targets.ColoredConsoleTarget() { Name = Scene };
            config.LoggingRules.Add(new NLog.Config.LoggingRule("*", LogLevel.Info, logconsole));
            config.LoggingRules.Add(new NLog.Config.LoggingRule("*", LogLevel.Debug, logfile));
            NLog.LogManager.Configuration = config;
            Logger = NLog.LogManager.GetLogger(Scene);
        }

        private string DateString()
        {
            return DateTime.Now.ToString("yyyyMMdd");
        }

        public void LogDebug(string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Debug(message);
        }

        public void LogDebug(Exception exception, string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Debug(exception, $"{message}\r\n错误信息:{exception.GetAllMessage()}");
        }

        public void LogError(string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Error(message);
        }

        public void LogError(Exception exception, string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Error(exception, $"{message}\r\n错误信息:{exception.GetAllMessage()}");
        }

        public void LogInformation(string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Info(message);
        }

        public void LogInformation(Exception exception, string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Info(exception, $"{message}\r\n错误信息:{exception.GetAllMessage()}");
        }

        public void LogWarning(string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Warn(message);
        }

        public void LogWarning(Exception exception, string message)
        {
            if (LogDate != DateString())
            { CinfigLog(); }
            Logger.Warn(exception, $"{message}\r\n错误信息:{exception.GetAllMessage()}");
        }
    }
}
